package com.jkl.databaseserver.Tool.RabbitMQ.Consumer;

import com.google.gson.Gson;
import com.jkl.databaseserver.Config.RabbitMQConfig;
import com.jkl.databaseserver.Web.DAO.pojo.Relationship;
import com.jkl.databaseserver.Web.Service.WsService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class Consumer {
    final WsService wsService;
    final Gson gson=new Gson();
    @Autowired
    public Consumer(WsService wsService) {
        this.wsService = wsService;
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_INSERT)
    public void receive1(String msgData, Message message, Channel channel) throws IOException {
        System.out.println("direct_insert消费者收到消息  : " + msgData);
        wsService.saveMessage(gson.fromJson(msgData, com.jkl.databaseserver.Web.DAO.pojo.Message.class));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_UPDATE)
    public void receive2(String msgData, Message message, Channel channel) throws IOException {
        System.out.println("direct_update消费者收到消息  : " + msgData);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_DELETE)
    public void receive3(String msgData, Message message, Channel channel) throws IOException {
        System.out.println("direct_delete消费者收到消息  : " + msgData);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
    @RabbitListener(queues = RabbitMQConfig.QUEUE_SEND)
    public void receive4(String msgData, Message message, Channel channel) throws IOException {
        System.out.println("direct_send收到消息  : " + msgData);
        wsService.buildRelationship(gson.fromJson(msgData, Relationship.class));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_AGREE)
    public void receive5(String msgData, Message message, Channel channel) throws IOException {
        System.out.println("direct_agree收到消息  : " + msgData);
        wsService.confirmRelationship(gson.fromJson(msgData, Relationship.class));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
